package world.criver.imagecompresstool.function;

import world.criver.imagecompresstool.data.daConfig;
import world.criver.imagecompresstool.data.daValue;

import java.io.*;

import static java.lang.StringTemplate.STR;

public class funReg {
    private String menu="ImageCompressTool_";
    private String parent=null;
    private String bootstrap_url="HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    private String content_menu_url="HKEY_CURRENT_USER\\SOFTWARE\\Classes\\*\\shell";
    public void change(){
        parent=daValue.bin_url;
        try {
            if (daConfig.bootstrap) addBootstrap();
            else deleteBootstrap();
            if(daConfig.content_menu)addContentMenu();
            else deleteContentMenu();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建开机自启动
     */
    private void addBootstrap() throws IOException {
        if(!exist(Runtime.getRuntime().exec(STR."reg query \{bootstrap_url}"),menu+"Bootstrap"))
            destroyProcess(Runtime.getRuntime().exec(STR. "reg add \{ bootstrap_url } /v ImageCompressTool_Bootstrap /t REG_SZ /d \{ parent }\\ImageCompressTool.exe" ));
    }

    /**
     * 删除开启自启动
     * @throws IOException
     */
    private void deleteBootstrap() throws IOException {
        destroyProcess(Runtime.getRuntime().exec(STR."reg delete \{bootstrap_url} /v ImageCompressTool_Bootstrap /f"));
    }

    /**
     * 添加上下文菜单
     * @throws IOException
     */
    private void addContentMenu() throws IOException {
        if(!exist(Runtime.getRuntime().exec(STR."reg query \{content_menu_url}"), menu+"ContentMenu")){
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu /v AppliesTo /t REG_SZ /d (System.ItemType:png) OR (System.ItemType:jpg)"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu /v MUIVerb /t REG_SZ /d 图片处理"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu /v icon /t REG_SZ /d \{parent}\\ic.ico"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu /v AppliesTo /t REG_SZ /d \"(System.ItemType:png) OR (System.ItemType:jpg)\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu /v SubCommands /t REG_SZ"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1 /v MUIVerb /t REG_SZ /d 图片压缩"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1 /v SubCommands /t REG_SZ"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu11 /v MUIVerb /t REG_SZ /d 普通压缩"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu11\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:compress1\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu12 /v MUIVerb /t REG_SZ /d 高质量压缩"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu12\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:compress2\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu13 /v MUIVerb /t REG_SZ /d 低质量压缩"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu1\\shell\\menu13\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:compress3\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2 /v MUIVerb /t REG_SZ /d 生成ico"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2 /v SubCommands /t REG_SZ"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu21 /v MUIVerb /t REG_SZ /d 自动尺寸"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu21\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico1\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu22 /v MUIVerb /t REG_SZ /d 16x16"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu22\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico2\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu23 /v MUIVerb /t REG_SZ /d 32x32"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu23\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico3\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu24 /v MUIVerb /t REG_SZ /d 48x48"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu24\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico4\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu25 /v MUIVerb /t REG_SZ /d 64x64"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu25\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico5\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu26 /v MUIVerb /t REG_SZ /d 128x128"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu26\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico6\" file:\"\"%1\"\"\""));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu27 /v MUIVerb /t REG_SZ /d 256x256"));
            destroyProcess(Runtime.getRuntime().exec(STR."reg add \{content_menu_url}\\\{menu}ContentMenu\\shell\\menu2\\shell\\menu27\\command /t REG_SZ /d \"\"\{parent}\\ImageCompressTool.exe\" \"mode:ico7\" file:\"\"%1\"\"\""));
        }
    }

    /**
     * 删除上下文菜单
     * @throws IOException
     */
    private void deleteContentMenu() throws IOException {
        destroyProcess(Runtime.getRuntime().exec(STR."reg delete \{content_menu_url}\\\{menu}ContentMenu /f"));
    }
    private void destroyProcess(Process p){
        try {
            p.waitFor();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if(p.isAlive())p.destroy();
    }
    private boolean exist(Process p,String key) throws IOException {
        BufferedReader re=new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
        String l;
        boolean ok=false;
        while((l=re.readLine())!=null) {
            if (l.contains(key)) {
                ok = true;
                break;
            }
        }
        p.destroy();
        return ok;
    }
}
